Page({
  data: {
    address: '点击选择，请勾选哦~',
    isSubmit: false,
    isSucc: false
  },

  onLoad() {
    this.staticData = {
      type: 'buy',
      contact: '',
      message: ''
    }
  },

  handleAddressTap() {
    wx.chooseLocation({
      success: (res) => {
        let { address, latitude, longitude } = res
        this.staticData.latitude = latitude
        this.staticData.longitude = longitude
        this.setData({
          address
        })
      }
    })
  },

  handleChange(e) {
    this.staticData.type = e.detail.value
  },

  handleContactInput(e) {
    this.staticData.contact = e.detail.value
  },

  handleMessageInput(e) {
    this.staticData.message = e.detail.value
  },

  _toast(title) {
    wx.showToast({
      title,
      icon: 'loading',
      duration: 2000
    })    
  },

  handleSubmitTap() {

    if(this.data.address === '点击选择，请勾选哦~') {
      this._toast('请选择地址')
      return
    }
    if(this.staticData.message === '') {
      this._toast('请填写说明')
      return
    }
    if(this.staticData.contact === '') {
      this._toast('请填写联系方式')
      return
    }

    let data = {
      ...this.staticData,
      ...this.data
    }

    wx.request({
      url: 'http://localhost:9000/data', //仅为示例，并非真实的接口地址
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: (res) => {
        this.setData({
          isSucc: true
        })
      },
      complete: () => {
        this.setData({
          isSubmit: true,
          isSucc: false
        })
      }
    })
  },


  handleBackTap() {
    wx.navigateBack()
  },

  handleCancelTap() {
    this.setData({
      isSubmit: false
    })
  }
})